package com.qu.waibao.my.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.alibaba.mobileim.login.YWLoginState;
import com.othershe.nicedialog.BaseNiceDialog;
import com.othershe.nicedialog.NiceDialog;
import com.othershe.nicedialog.ViewConvertListener;
import com.othershe.nicedialog.ViewHolder;
import com.qu.waibao.AppConfig;
import com.qu.waibao.R;
import com.qu.waibao.account.LoginActivity;
import com.qu.waibao.account.RegisterActivity;
import com.qu.waibao.base.BaseActivity;
import com.qu.waibao.base.BaseRecyclerAdapter;
import com.qu.waibao.home.adpater.KefuAdapter;

import butterknife.BindView;
import butterknife.OnClick;

public class AccountManagerActivity extends BaseActivity implements View.OnClickListener{
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_publish)
    ImageView iv_publish;
    @Override
    protected int getContentView() {
        return R.layout.activity_account_manager;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        tv_title.setText("账户管理");
        iv_publish.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @OnClick({R.id.ll_back,R.id.rl_xiugai,R.id.tv_quit,R.id.tv_about_me})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_about_me:
                startActivity(new Intent(this,AboutMeActivity.class));
                break;
            case R.id.rl_xiugai:
                Intent intent =new Intent(this, RegisterActivity.class);
                intent.putExtra("type",3);
                startActivity(intent);
                break;
            case R.id.tv_quit:
                NiceDialog.init()
                        .setLayoutId(R.layout.confirm_dialog_layout)     //设置dialog布局文件
                        .setConvertListener(new ViewConvertListener() {     //进行相关View操作的回调
                            @Override
                            public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                                holder.setText(R.id.tv_content,"您确定要退出吗？");
                                holder.setText(R.id.tv_title,"退出操作");
                                holder.setOnClickListener(R.id.tv_sure, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        dialog.dismiss();
                                       /* startActivity(new Intent(AccountManagerActivity.this, LoginActivity.class));
                                        finish();*/
                                        logout();
                                    }
                                });
                                holder.setOnClickListener(R.id.tv_cancel, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        dialog.dismiss();
                                    }
                                });
                            }
                        })
                        .setMargin(30)
                        .setDimAmount(0.3f)     //调节灰色背景透明度[0-1]，默认0.5f
                        .setShowBottom(false)     //是否在底部显示dialog，默认flase
                        .setOutCancel(true)     //点击dialog外是否可取消，默认true
                        .show(getSupportFragmentManager());     //显示dialog
                break;

        }
    }

    public void logout() {
        // openIM SDK提供的登录服务
        YWIMKit mIMKit = YWAPI.getIMKitInstance(AppConfig.getString("userid"), AppConfig.APP_KEY);
        IYWLoginService mLoginService = mIMKit.getLoginService();
        mLoginService.logout(new IWxCallback() {
            @Override
            public void onSuccess(Object... arg0) {
                Toast.makeText(AccountManagerActivity.this, "退出成功", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(AccountManagerActivity.this, LoginActivity.class));
                finish();
            }

            @Override
            public void onProgress(int arg0) {

            }

            @Override
            public void onError(int arg0, String arg1) {
                Toast.makeText(AccountManagerActivity.this, "退出失败,请重新登录", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
